$(document).ready(function() {
	showUser();
	showSxks();
	$('#ss').searchbox({
		searcher : function(value, name) {
			showUser(value, name);
		},
		menu : '#mm',
		prompt : '请输入你要查找的姓手机号'
	});
});

function showSxks() {
	$('#sxks').combobox({
		url : "ksmanage_showKsList.do",
		valueField : 'id',
		textField : 'dname'
	});
}
function showUser(value, name) {
	$("#userRole")
			.datagrid(
					{
						url : "login_userlist.do?flag=1&value=" + value
								+ "&name=" + name,
						rownumbers : true,
						pagination : true,
						rownumber : true,
						fitColumns : true,
						striped : true,
						collapsible : true,
						remoteSort : false,
						nowrap : false,
						checkOnSelect : false,
						singleSelect : true,
						loadMsg : '数据加载中，请稍等...',
						columns : [ [
								{
									field : 'name',
									title : '姓名',
									width : 100,
									align : 'center'
								},
								{
									field : 'zymc',
									title : '类别',
									width : 100,
									align : 'center',
									formatter : function(value, rowData,
											rowIndex) {
										return rowData.isdoc == 1 ? "医生" : "病人";
									}
								},
								{
									field : 'mobile',
									title : '手机号码',
									width : 100,
									align : 'center'
								},
								{
									field : 'update',
									title : '操作',
									width : 100,
									align : 'center',
									formatter : function(value, rowData,
											rowIndex) {
										return "<a href='javascript:void(0);' onclick='_update("
												+ parseInt(rowData.id)
												+ ")'>修改</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='javascript:void(0);' onclick='userDelete("
												+ parseInt(rowData.id)
												+ ")'>删除</a>&nbsp;&nbsp;<a href='javascript:void(0);' onclick='roleUpdate("
												+ parseInt(rowData.id)
												+ ","
												+ '"'
												+ rowData.name
												+ '"'
												+ ")'>指派权限</a>";
									}
								} ] ]
					});
}

function _update(id) {
	$.post("login_update.do", {
		"id" : id
	}, function(data) {
		if (data.userDo != null) {
			$("input[name='name']").val(data.userDo.name);
			$("input[name='name']").attr("readonly","readonly");
			if (data.userDo.sex == 1) {
				$("#man").attr("checked", "checked")
			} else {
				$("#woman").attr("checked", "checked")
			}
			$("input[name='age']").val(data.userDo.age);
			if (data.userDo.isdoc == 1) {
				$("#zy").combobox("setValue", "医生");
			} else {
				$("#zy").combobox("setValue", "病人");
			}
			$("#sxks").combobox("setValue", data.userDo.dname);
			$("input[name='mobile']").val(data.userDo.mobile);
			$("input[name='email']").val(data.userDo.email);
			$("input[name='address']").val(data.userDo.address);
			$("#opt").val("opt");
			showDialog(1);
		} else {
			$.messager.alert("提示", "该姓名已被使用");
		}
	}, "json");
}

function user_save() {
	var name = $("input[name='name']").val();
	var _sex = $("#woman").is(":checked");
	var sex = 1;
	if (_sex) {
		sex = 2;
	}
	var age = $("input[name='age']").val();
	var zy = $("#zy").combobox("getValue");
	var sxks = $("#sxks").combobox("getValue");
	var mobile = $("input[name='mobile']").val();
	var email = $("input[name='email']").val();
	var address = $("input[name='address']").val();
	if (name.trim() == "" || age.trim() == ""
			|| zy.trim() == "" || sxks.trim() == "" || mobile.trim() == ""
			|| email.trim() == "" || address.trim() == "") {
		$.messager.alert("提示", "请填写完整");
		return;
	}
	if (CheckMail(email)) {
		$.post("login_userSave.do", {
			"name" : name,
			"sex" : sex,
			"age" : age,
			"zy" : zy,
			"sxks" : sxks,
			"mobile" : mobile,
			"email" : email,
			"address" : address,
			"opt":$("#opt").val()
		}, function(data) {
			if (data.success == "success") {
				$.messager.alert("提示", "操作成功");
				showUser();
				$("#add_user").dialog({
					closed : true
				});
			} else {
				$.messager.alert("提示", "该姓名已被使用");
			}
		}, "json");
	}
}

function CheckMail(mail) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(mail))
		return true;
	else {
		$.messager.alert("提示", "您的电子邮件格式不正确");
		return false;
	}
}

function _save() {
	var uid = $("#uid").val();
	var power = $("#tempPower").combobox("getValue");
	if (uid == null || power == null || uid == "" || power == "") {
		$.messager.alert("提示", "参数错误");
	} else {
		$.post("powermanage_update.do", {
			"uid" : uid,
			"power" : power
		}, function(data) {
			if (data.success == "success") {
				$.messager.alert("提示", "操作成功");
				showUser();
				$("#add_role").dialog({
					closed : true
				});
			} else {
				$.messager.alert("提示", "操作失败");
			}
		}, "json");
	}
}

function roleUpdate(id, name) {
	$("#uid").val(id);
	$("#user_combobox").text(name);
	$("#add_role").dialog({
		closed : false
	});

}

function userDelete(id) {
	$.post("login_delete.do", {
		"id" : id
	}, function(data) {
		if (data.success == "success") {
			$.messager.alert("提示", "操作成功");
			showUser();
		} else {
			$.messager.alert("提示", "操作失败");
		}
	}, "json");
}

function showDialog(opt) {
	if (opt != "1") {
		$("input[name='name']").val("");
		$("input[name='sex']").val("");
		$("input[name='age']").val("");
		$("#zy").combobox("setValue", "");
		$("#sxks").combobox("setValue", "");
		$("input[name='mobile']").val("");
		$("input[name='email']").val("");
		$("input[name='address']").val("");
		$("#opt").val("");
	}
	$("#add_user").dialog({
		closed : false
	});
}